import axios from "axios";

// 添加操作日志的通用函数
export const addOperationLog = async (action) => {
  try {
    const time = new Date()
      .toLocaleString("zh-CN", {
        year: "numeric",
        month: "2-digit",
        day: "2-digit",
        hour: "2-digit",
        minute: "2-digit",
        second: "2-digit",
        hour12: false,
      })
      .replace(/\//g, "-");

    // 获取当前最大ID并递增
    const logsRes = await axios.get(
      "http://localhost:3000/operationLogs?_sort=id&_order=desc&_limit=1"
    );
    const maxId = logsRes.data.length > 0 ? parseInt(logsRes.data[0].id) : 0;
    const newId = maxId + 1;

    await axios.post("http://localhost:3000/operationLogs", {
      id: newId,
      action,
      time,
    });
  } catch (error) {
    console.error("记录操作日志失败:", error);
  }
};
